package com.realestate.services;

import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.realestate.constants.AppConstant;
import com.realestate.constants.ComTxManagerName;
import com.realestate.dao.ProductQueryDao;
import com.realestate.model.BrtbProduct;

@Service
public class ProductServiceImpl implements ProductService{
	
	@SuppressWarnings("unused")
	private static final Logger LOGGER = Logger.getLogger(ProductServiceImpl.class);
	
	@Autowired
    private ProductQueryDao productQueryDao;

	@Override
	public List<BrtbProduct> getListproductByCategoryCode(String categoryCode, Long limit) {
		return productQueryDao.getListproductByCategoryCode(categoryCode, limit);
	}

	@Override
	@Transactional(value = ComTxManagerName.COM_TRANSACTION_MANAGER)
	public BrtbProduct mergeProduct(BrtbProduct product) {
		BrtbProduct productResult = productQueryDao.merge(product);
		productQueryDao.flush();
		productResult.setProductCode(setProductCode(productResult));
		return productResult;
	}

	private String setProductCode(BrtbProduct product) {
		StringBuffer productCode = new StringBuffer();
		productCode.append(product.getBrtbDistrict().getDistrictCode() + "_" + product.getBrtbCategory().getCategoryCode() + "_" + product.getId());
		return productCode.toString();
	}
	
	@Override
	public Page<BrtbProduct> getAllProduct(Pageable pageable) {
		return productQueryDao.getAllProduct(pageable);
	}

	@Override
	@Transactional(value = ComTxManagerName.COM_TRANSACTION_MANAGER)
	public String changeRecordStatus(Long id, String recordStatus) {
		BrtbProduct product = productQueryDao.find(id);
		product.setRecordStatus(recordStatus);
		BrtbProduct result = productQueryDao.save(product);
		if(result != null && result.getId() == product.getId() && result.getRecordStatus() == product.getRecordStatus()){
			return AppConstant.SUCCESS;
		}
		return AppConstant.FAIL;
	}

	@Override
	public BrtbProduct findProductById(Long id) {
		return productQueryDao.find(id);
	}

	@Override
	public String getProductAttrbuteValueByCode(Long productId, String attributeCode) {
		return productQueryDao.getProductAttrbuteValueByCode(productId, attributeCode);
	}

	@Override
	@Transactional(value = ComTxManagerName.COM_TRANSACTION_MANAGER)
	public void updatePictureProduct(Long productId, String picture) {
		BrtbProduct persist = productQueryDao.find(productId);
		persist.setPicture(persist.getPicture() + picture);
		productQueryDao.flush();
	}

	@Override
	@Transactional(value = ComTxManagerName.COM_TRANSACTION_MANAGER)
	public void resetPictureProduct(Long productId) {
		BrtbProduct persist = productQueryDao.find(productId);
		persist.setPicture("");
		productQueryDao.flush();
	}
}
